package cn.maxwell.web.controller;

import cn.maxwell.query.MovieTicketQuery;
import cn.maxwell.result.JsonResult;
import cn.maxwell.result.PageList;
import cn.maxwell.service.IMovieTicketService;
import cn.maxwell.domain.MovieTicket;
import com.baomidou.mybatisplus.plugins.Page;
import io.swagger.annotations.ApiImplicitParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/movieTicket")
public class MovieTicketController {

    @Autowired
    public IMovieTicketService movieTicketService;

    /**
    * 保存和修改公用的
    */
    @RequestMapping(value="/save",method= RequestMethod.POST)
    public JsonResult saveOrUpdate(@RequestBody MovieTicket movieTicket){
        if(movieTicket.getId()!=null){
            movieTicketService.updateById(movieTicket);
        }else{
            movieTicketService.insert(movieTicket);
        }
        return JsonResult.success();
    }

    /**
    * 删除对象
    */
    @RequestMapping(value="/{id}",method=RequestMethod.DELETE)
    public JsonResult delete(@PathVariable("id") Long id){
        movieTicketService.deleteById(id);
        return JsonResult.success();
    }

    /**
   * 获取对象
   */
    @RequestMapping(value = "/{id}",method = RequestMethod.GET)
    public JsonResult get(@PathVariable("id")Long id){
        return JsonResult.success(movieTicketService.selectById(id));
    }


    /**
    * 查询所有对象
    */
    @RequestMapping(value = "/list",method = RequestMethod.GET)
    public JsonResult list(){
        return JsonResult.success(movieTicketService.selectList(null));
    }


    /**
    * 带条件分页查询数据
    */
    @RequestMapping(value = "/pagelist",method = RequestMethod.POST)
    public JsonResult page(@RequestBody MovieTicketQuery query){
        Page<MovieTicket> page = new Page<MovieTicket>(query.getPage(),query.getRows());
        page = movieTicketService.selectPage(page);
        return JsonResult.success(new PageList<MovieTicket>(page.getTotal(),page.getRecords()));
    }

    /**
     * 电影票购买根据Id进行购买，购买前判断库存是否足够，如果不足响应提示，购买需要保存用户、电影票中间表，以及是否使用状态
     */
    @GetMapping(value = "byMovieTicket/{id}/{userId}/{count}")
    public JsonResult byMovieTicket(@PathVariable("id") Long id,@PathVariable("userId") Long userId,@PathVariable("count") Integer count){
        movieTicketService.byMovieTicket(id,userId,count);
        return JsonResult.success();
    }

    /**
     * 观看电影
     * @param id
     * @param userId
     * @return
     */
    @GetMapping(value = "see/{id}/{userId}")
    public JsonResult seeMovie(@PathVariable("id") Long id,@PathVariable("userId") Long userId){
        movieTicketService.seeMovie(id,userId);
        return JsonResult.success();
    }

}
